<template>
  <div class="icons">
    <swiper>
      <swiper-slide v-for="(pageindex,index) in pages" :key="index">
         <div class="icon-item" v-for="item of pageindex" :key="item.id">
            <img :src="item.imgurl" alt="">
            <p>{{item.title}}</p>
         </div>
      </swiper-slide>
    </swiper>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'HomeIcons',
  data () {
    return {
      iconlist: []
    }
  },
  beforeMount () {
    axios.get('/api/static/mock/icons.json').then((res) => {
      const promisedata = res.data
      this.iconlist = promisedata
    })
  },
  computed: {
    pages () {
      const pages = []
      this.iconlist.forEach((item, index) => {
        const page = Math.floor(index / 8)
        if (!pages[page]) {
          pages[page] = []
        }
        pages[page].push(item)
      })
      return pages
    }
  }
}
</script>

<style lang="scss" scoped>
  .icons{
    width: 100%;
    overflow: hidden;
    height: 0;
    padding-bottom: 50%;
    margin-top: .1rem;
    .icon-item{
      width: 25%;
      height: 0;
      padding-bottom: 25%;
      float: left;
      text-align: center;
      img{
        width: 70%;
        box-sizing: border-box;
        margin: 3px;
        margin-bottom: 6px;
      }
      p{
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
    }
  }
</style>
